

const TIME_ONE = 60000;
const TIME_TWO = 1000;
const TIME_THREE = 10;

export function timeConvert(time: number): string {
  let min: number = Math.floor(time / TIME_ONE);
  let second: string = ((time % TIME_ONE) / TIME_TWO).toFixed(0);
  return `${min}:${(+second < TIME_THREE ? '0' : '') + second}`;
}

class TimeUtil {
  /**
   * Seconds converted to HH:mm:ss.
   *
   * @param seconds Maximum video duration (seconds).
   * @return Time after conversion.
   */
  secondToTime(seconds: number): string {
    let hourUnit = 60 * 60;
    let hour: number = Math.floor(seconds / hourUnit);
    let minute: number = Math.floor((seconds - hour * hourUnit) / 60);
    let second: number = seconds - hour * hourUnit - minute * 60;
    let hourStr: string = hour < 10 ? `0${hour.toString()}` : `${hour.toString()}`
    let minuteStr: string = minute < 10 ? `0${minute.toString()}` : `${minute.toString()}`
    let secondStr: string = second < 10 ? `0${second.toString()}` : `${second.toString()}`
    if (hour > 0) {
      return `${hourStr}:${minuteStr}:${secondStr}`;
    }
    if (minute > 0) {
      return `${minuteStr}:${secondStr}`;
    } else {
      return `00:${secondStr}`;
    }
  }
}

export const TIME_UTIL = new TimeUtil();
